﻿using BaseUI;
using BLL;
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Thksoft.Module.DataDetail
{
    public partial class BankListDetail : BaseUI.BasePage
    {
        private Sys_CompanyBLL companyBLL = new Sys_CompanyBLL();
        private Sys_BankBLL bankBLL = new Sys_BankBLL();
        private YX_BANK_LISTBLL bll = new YX_BANK_LISTBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtDate.Value = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd");

                txtEnd.Value = DateTime.Now.ToString("yyyy-MM-dd");

                ViewState["SortOrder"] = "n_detail_listid";
                ViewState["OrderDire"] = " desc ";

                BindBank();
                bindN_BANK();
                Bind();
            }
        }

        /// <summary>
        /// 绑定银行
        /// </summary>
        public void BindBank()
        {
            DataTable dt = bankBLL.SelBank();

            PublicMethod.BindDDL(ddlBank, dt, "N_ID", "C_NAME", true);
        }

        /// <summary>
        /// 绑定银行账号
        /// </summary>
        public void bindN_BANK()
        {
            DataTable dt = bankBLL.SelC_ACCNO2();
            PublicMethod.BindDDL(N_BANK_ID, dt, "N_ID", "C_NUMBER", true);
        }

        public void BindData(bool isFirst)
        {
            string Where = "";
            try
            {
                Where = ViewState["Where"].ToString();
            }
            catch
            {
                Where = @"  and  t.n_company_id in (select cp.cmy_id
  from sys_company cp
 start with cp.cmy_id = " + CurrentUserCompanyID + @"
connect by prior cp.cmy_id = cp.cmy_parentid) ";
            }

            int rowcount = 0;
            if (isFirst)
            {
                PageIng.CurrentPage = 1;
            }
            //合计金额
            decimal amt = 0;

            decimal inje = 0;
            decimal outje = 0;
            rowcount = bll.GetListReCount(out amt, out inje, out outje, Where, lognUser.USER_NAME);

            string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];

            DataTable dt = bll.GetListReByPage(Where, sort, PageIng.CurrentPage, PageIng.RowsPerPage);

            if (dt.Rows.Count > 0)
            {
                string zje = dt.Compute("Sum(n_amt)", "").ToString();

                Label1.Text = "总金额：" + Utility.Helper.HtmlHelper.QyFormat(amt, 2) + "元，收入：" + Utility.Helper.HtmlHelper.QyFormat(inje, 2) + "元，支出：" + Utility.Helper.HtmlHelper.QyFormat(outje, 2) + @"元；本页金额：" + Utility.Helper.HtmlHelper.QyFormat(Convert.ToDecimal(zje), 2) + "元";
                //Label1.Text = "总金额：" + amt.ToString("N") + "元，收入：" + inje.ToString("N") + "元，支出：" + outje.ToString("N") + @"元；本页金额：" + Utility.Helper.HtmlHelper.QyFormat(Convert.ToDecimal(zje), 2) + "元";
                Label1.Visible = true;
                PageIng.Visible = true;
                PageIng.RowsCount = rowcount;
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow1;
                PageIng.RowsPerPage = PageIng._rowsPerPage;
                PageIng.Bind();
            }
            else
            {
                PageIng.DataSource = dt;
                PageIng.GridViewSource = grvShow1;
                grvShow1.DataSource = null;
                grvShow1.DataBind();
                PageIng.Visible = false;
                Label1.Visible = false;
            }
        }

        public void PageIng_DataBinding(object sender, EventArgs e)
        {
            BindData(false);
        }

        protected void QUERY_CLICK(object sender, EventArgs e)
        {
        }

        protected void grvShow_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    e.Row.Attributes.Add("onmouseover", "cu=style.backgroundColor;style.backgroundColor='#d5e7ff'");
            //    e.Row.Attributes.Add("onmouseout", "style.backgroundColor=cu");
            //}
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "cu=style.backgroundColor;style.backgroundColor='#d5e7ff'");
                e.Row.Attributes.Add("onmouseout", "style.backgroundColor=cu");
                if (e.Row.RowIndex % 2 == 0)
                {
                    e.Row.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#FFFFFF");
                }
                else
                {
                    e.Row.Style.Add(HtmlTextWriterStyle.BackgroundColor, "#EAFFFF");
                }
            }
        }

        protected void grvShow1_Sorting(object sender, GridViewSortEventArgs e)
        {
            //点击排序
            string sPage = e.SortExpression;

            if (sPage == "")
            {
                return;
            }
            if (ViewState["SortOrder"].ToString() == sPage)
            {
                if (ViewState["OrderDire"].ToString() == "Desc")
                    ViewState["OrderDire"] = "ASC";
                else
                    ViewState["OrderDire"] = "Desc";
            }
            else
            {
                ViewState["SortOrder"] = e.SortExpression;
                ViewState["OrderDire"] = "ASC";
            }

            BindData(true);
        }

        /// <summary>
        /// 导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnPrint_Click(object sender, EventArgs e)
        {
            if (grvShow1 == null || grvShow1.Rows.Count == 0)
            {
                MessageBox.Show(Page, "【系统提示】未能查询出数据，暂时不能导出。");
                return;
            }

            toExecl1(Panel1, "银行流水明细" + DateTime.Now.ToString("yyyy-MM-dd") + DateTime.Now.Millisecond.ToString());
        }

        private void Bind()
        {
            string whereStr = "";

            //公司
            if (FORM_COMPANY_ID.Value != -1)
            {
                whereStr += " and t.n_company_id=" + FORM_COMPANY_ID.Value;
            }
            else
            {
                whereStr += @"  and t.n_company_id in (select cp.cmy_id
  from sys_company cp
 start with cp.cmy_id = " + lognUser.USER_COMPANY + @"
connect by prior cp.cmy_id = cp.cmy_parentid) ";
            }

            if (txtDate.Value.Trim() != "" && txtEnd.Value.Trim() != "")
            {
                whereStr += @"   and t.d_trandate between
       TO_DATE('" + txtDate.Value + @"', 'YYYY-MM-DD') and
       TO_DATE('" + txtEnd.Value + @"', 'YYYY-MM-DD') ";
            }

            if (ddlBank.SelectedItem.Value != "-1")
            {
                whereStr += " and b.c_name = '" + ddlBank.SelectedItem.Text + "'";
            }
            if (N_BANK_ID.SelectedItem.Value != "-1")
            {
                whereStr += "and t.C_ACCNO2 ='" + N_BANK_ID.SelectedItem.Text + "'";
            }

            ViewState["Where"] = whereStr;

            BindData(true);
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            Bind();
        }

        protected void PRINT_CLICK(object sender, EventArgs e)
        {
            if (grvShow1 == null || grvShow1.Rows.Count == 0)
            {
                MessageBox.Show(Page, "【系统提示】未能查询出数据，暂时不能打印。");
                return;
            }

            string whereStr = "";

            //公司
            if (FORM_COMPANY_ID.Value != -1)
            {
                whereStr += " and t.n_company_id=" + FORM_COMPANY_ID.Value;
            }
            else
            {
                whereStr += @"  and t.n_company_id in (select cp.cmy_id
  from sys_company cp
 start with cp.cmy_id = " + lognUser.USER_COMPANY + @"
connect by prior cp.cmy_id = cp.cmy_parentid) ";
            }

            if (txtDate.Value.Trim() != "" && txtEnd.Value.Trim() != "")
            {
                whereStr += @"   and t.d_trandate between
       TO_DATE('" + txtDate.Value + @"', 'YYYY-MM-DD') and
       TO_DATE('" + txtEnd.Value + @"', 'YYYY-MM-DD') ";
            }

            PageIng.CurrentPage = 1;

            string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];

            DataTable dt = bll.GetListReByPage(whereStr, sort, PageIng.CurrentPage, PageIng.RowsPerPage);

            dt.Columns["r"].ColumnName = "序号";
            dt.Columns["cmy_name"].ColumnName = "公司";
            dt.Columns["C_ACCNO1"].ColumnName = "账号";
            dt.Columns["c_accno2"].ColumnName = "对方账号";
            dt.Columns["C_ACC_NAME1"].ColumnName = "对方账号名称";
            dt.Columns["c_name"].ColumnName = "银行";
            dt.Columns["d_trandate"].ColumnName = "日期";
            dt.Columns["n_amt"].ColumnName = "发生额";
            dt.Columns["n_flag1"].ColumnName = "借贷标志";
            dt.Columns["c_det"].ColumnName = "备注";
            //dt.Columns.Remove("r");
            dt.Columns.Remove("C_MESSAGE");
            dt.Columns.Remove("n_amt1");

            var key = Guid.NewGuid().ToString();
            Session[key + "Header"] = "银行流水明细";
            Session[key + "Body"] = dt;

            MessageBox.ResponseScript(this, "top.MessageBox.Print({Value:'ID=BASIC&VALUE=" + key + "'})");
        }
    }
}